// app.js
const request = require("./utils/request.js");
App({
	onLaunch() {
		
		// 展示本地存储能力
		const logs = wx.getStorageSync('logs') || []
		logs.unshift(Date.now())
		wx.setStorageSync('logs', logs)
		const HOST=this.globalData.HOST;
		if(wx.getStorageSync("openid")==""){
			// 登录
			wx.login({
				success: res => {
					// 发送 res.code 到后台换取 openId, sessionKey, unionId
					wx.request({
						url: HOST+"wxphp/wxapi.php?wxcode=" + res.code,
						success: (res) => {
							wx.setStorageSync('openid', res.data.openid)
							wx.setStorageSync('session', res.data.session_key)
							console.log(res)
							request.post(HOST+"wxphp/reg.php", {
								wx: res.data.openid
							}).then((res) => {
								console.log(res)
							})
						}
					})
					// console.log(res.code)
				}
			})
		}
		// wx.checkSession({
		// 	success() {
		// 		//session_key 未过期，并且在本生命周期一直有效
		// 		console.log(wx.getStorageSync("openid"))
		// 	},
		// 	fail() {
		// 		// session_key 已经失效，需要重新执行登录流程
				
		// 	}
		// })

	},
	globalData: {
		userInfo: null,
		HOST: "https://pc.aizhaiyu.com/",
		BBS: "https://bbs.aizhaiyu.com/",
		request: request
	}
})
